package com.example.demo.dao;


import com.example.demo.entity.Order;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author liuxianchun
 * @date 2021/1/21
 */
@Repository
public interface OrderDao {

    /*从购物车结算*/
    @Insert("insert into orders(orderId,userId,productId,productNum,productPrice,orderTime,payTime,status,payStatus) values(#{orderId},#{userId},#{productId},#{productNum},#{productPrice},now(),now(),1,1)")
    void addOrder(Long orderId,int userId, int productId, int productNum, BigDecimal productPrice);

    @Select("select * from orders where orderId=#{orderId}")
    List<Order> getOrders(Long orderId);

    @Select("select distinct orderId from orders where userId=#{userId} order by orderId desc")
    List<Long> getOrderIdByUser(int userId);

    @Select("select getTransNum('order')")
    Long getOrderId();

    /*秒杀生成订单*/
    @Insert("insert into orders(orderId,userId,productId,productNum,productPrice,orderTime,status,payStatus) values(#{orderId},#{userId},#{productId},#{productNum},#{productPrice},now(),1,0)")
    void addOrderNoPay(Long orderId,int userId, int productId, int productNum, BigDecimal productPrice);

    /*付款*/
    @Update("update orders set payTime=now(),payStatus=1 where userId=#{userId} and orderId=#{orderId}")
    int orderPay(Integer userId, Long orderId);
}
